package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.common.Result;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Type;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.ITypeService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 十七
 * @since 2022-11-21
 */
@RequiresPermissions("类型管理")
@RestController
@RequestMapping("/type")
public class TypeController {
    
        @Resource
        private ITypeService typeService;

        // 新增或者更新
        @MyLog(value = "新增或者更新物品类型")
        @PostMapping
        public Result save(@RequestBody Type type) {
                return Result.success(typeService.saveOrUpdate(type));
                }

        @MyLog(value = "单个删除物品类型")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(typeService.removeById(id));
                }

        @MyLog(value = "批量删除物品类型")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(typeService.removeByIds(ids));
                }

        @MyLog(value = "查询所有物品类型")
        @GetMapping
        public Result findAll() {
                return Result.success(typeService.list());
                }

        @MyLog(value = "单个查询物品类型")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(typeService.getById(id));
                }

        @MyLog(value="分页查询物品类型")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String typename) {
                QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
//                queryWrapper.orderByDesc("id");
                if (!"".equals(typename)) {
                        queryWrapper.like("typename", typename);
                }
                return Result.success(typeService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }

        }

